package cn.syw.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.syw.domain.Permission;
import cn.syw.service.PermissionServiceImpl;
import cn.syw.service.iface.PermissionService;
@WebFilter(dispatcherTypes = {DispatcherType.FORWARD},urlPatterns = { "/views/main.jsp" })
public class PermissionCheckFilter implements Filter {
	
	public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	    HttpServletRequest request =(HttpServletRequest)req;
	    HttpSession session=request.getSession();
	    if(session.getAttribute("name")!=null) {
	        PermissionService permissionService=new PermissionServiceImpl();
	        System.out.println("权限查询完毕！");
	        List<Permission> permissions=permissionService.getPermissions((String)session.getAttribute("name"));
	        session.setAttribute("permissions", permissions);
	    }
	    chain.doFilter(req, response); 
	}

	
	public void init(FilterConfig fConfig) throws ServletException {
	}


	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}
